import dataList from "@/components/dataList";
import { fetchList, delObj } from "../../../api/finance-collection-actual";
import editRefund from "../edit/edit-refund";
import receiptData from "../edit/receipt-data";
import billData from "../edit/bill-data";
import depositsData from "../edit/deposits-data";
import leadData from "../edit/lead-data";
export default {
    name: "resource-manage",
    components: {
        dataList,
        editRefund,
        billData,
        depositsData,
        receiptData,
        leadData
    },
    provide() {
        return {
            initList: this.getListData
        };
    },
    beforeMount() {
        this.getListData();
    },
    data() {
        return {
            tableData: [],
            // 请求参数
            params: {
                current: 1,
                size: 20,
                projectName: ""
            },
            // 分页
            pager: {
                total: 0, // 总页数
                current: 1, // 当前页数
                size: 20 // 每页显示多少条
            },
            // 页面loading
            loading: false,
            filterList: [
                {
                    label: "费用期间",
                    inputType: "daterange",
                    name: ["costPeriodStart", "costPeriodEnd"]
                },
                { label: "收款日期", inputType: "date", name: "receivableDate" }
            ],
            selectionList: [],
            limitUpload: 1,
            fileTemp: []
        };
    },
    filters: {
        mySubString(val) {
            if (val != null) {
                return val.substring(5, 7);
            }
            return "";
        }
    },
    methods: {
        getListData(params = {}) {
            this.loading = true;
            let requestData = { ...this.params, ...params };
            fetchList(requestData)
                .then(res => {
                    const { current, size, total, records } = res.data.data;
                    this.tableData = records;
                    this.params = requestData;
                    this.pager = { total, current, size };
                })
                .finally(() => {
                    this.loading = false;
                });
        },
        //导入
        handleLeadingIn() {
            this.$refs.leadData.show();
        },
        editRefund() {
            if (this.selectionList.length === 1) {
                this.$refs.editRefund.show(this.selectionList[0]);
            } else {
                this.err("请选择一个要收款的项目");
            }
        },
        bill() {
            if (this.selectionList.length === 1) {
                this.$refs.billData.show(this.selectionList[0]);
            } else {
                this.err("请选择一个要打印的项目");
            }
        },
        deposits() {
            if (this.selectionList.length === 1) {
                if (
                    this.selectionList[0].chargeProjectName.split("押金")
                        .length > 1
                ) {
                    this.$refs.depositsData.show(this.selectionList[0]);
                } else {
                    this.err("请选择一个为押金的项目");
                }
            } else {
                this.err("请选择一个要打印的项目");
            }
        },
        receipt() {
            if (this.selectionList.length === 1) {
                this.$refs.receiptData.show(this.selectionList[0]);
            } else {
                this.err("请选择一个要开收据的项目");
            }
        },
        invalid() {
            if (this.selectionList.length > 0) {
                let ids = this.selectionList.map(item => item.actualId);
                delObj(ids.join(",")).then(resp => {
                    if (resp.data.data) {
                        this.suc("删除成功");
                        this.getListData();
                    } else {
                        this.err("删除失败");
                    }
                });
            } else {
                this.err("请至少选择一个要作废的项目");
            }
        },
        downloadData() {
            this.publicExport(
                "实收",
                "/api/financecollectionactual/download",
                this.searchForm
            );
        },
        // 菜单点击监听
        handleMenuClick(type) {
            switch (type) {
                case "test":
                    this.editRefund();
                    break;
                case "test1":
                    this.receipt();
                    break;
                case "test2":
                    this.deposits();
                    break;
                case "export":
                    this.downloadData();
                    break;
                default:
                    this.$message(type);
                    break;
            }
        }
    }
};
